import { ref } from 'vue';
import { CEventItem, CEventProcessItem, CVariableDataItem } from 'stores/editorModels';
import { useCatalogTool } from 'src/hook/common/useCatalogTool';
import { cataItem } from 'src/hook/common/useCatalogType';


export function useEvent () {
  //事件队列
  let myEventList = ref<CEventProcessItem[]>([]);

  //*********************************************************************************************
  //      读取目录
  //*********************************************************************************************
  const {
    findCatalogLabel
  } = useCatalogTool();

  //读取数据库
  const readEventVariableTbl = (varTbl: CVariableDataItem[], varId: number): number | null => {
    let index = varTbl.findIndex((item) => (item.varId === varId));
    if (index !== -1) {
      return varTbl[index].vData;
    } else {
      return null;
    }
  }

  //配置动效
  const configEvent = (list: CEventItem[], eventTypeArr: cataItem[], operationTypeArr: cataItem[],
    operatorTypeArr: cataItem[]) => {
    myEventList.value.length = 0;
    list.forEach((item, index) => {
      let el = {
        eventType: findCatalogLabel(eventTypeArr, item.typeId),
        operationType: findCatalogLabel(operationTypeArr, item.operationId),
        variation: {
          variableId: 0,
          operator: '',
          lowArg: 0,
          highArg: 0
        },
        url: item.url,
        msg: ''
      };

      //运算符信息
      if ((item.operatorId) && (item.variableId) &&
        (item.lowArg) && (item.highArg)) {
        el.variation.operator = findCatalogLabel(operatorTypeArr, item.operatorId);
        el.variation.variableId = item.variableId;
        el.variation.lowArg = item.lowArg;
        el.variation.highArg = item.highArg;
      }

      //信息
      if (item.msg) {
        el.msg = item.msg;
      }

      //加入数组
      myEventList.value.push(el);
    });
  }


  return {
    myEventList,
    readEventVariableTbl,
    configEvent
  }
}
